package com.xquant.trade.trade.service.impl;

import com.xquant.trade.trade.service.DayEndService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DayEndServiceFactory implements InitializingBean {

    @Autowired
    private List<DayEndService> services;
    private Map<String, DayEndService> serviceMap;

    public DayEndService getDayEndService(String assetType) {
        if (serviceMap.containsKey(assetType)) {
            return serviceMap.get(assetType);
        }
        return serviceMap.get("default");
    }

    public List<DayEndService> getServices() {
        return services;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        serviceMap = new HashMap<>();
        for (int i = 0; i < services.size(); i++) {
            DayEndService service = services.get(i);
            for (int j = 0; j < service.getAssetTypes().length; j++) {
                serviceMap.put(service.getAssetTypes()[j], service);
            }
        }
    }
}
